<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购物车</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"/>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
    <meta name="referrer" content="no-referrer">
    <style>
        img{
            width: 60px;
            height: 80px;
        }
        .count{
            width: 50px;
        }
    </style>
</head>
<body>
    <div id="app" class="container">
        
        <table class="table table-hover">
            <thead>
                <tr>
                    <th>全选<input 
                            type="checkbox" 
                            v-model="allChecked"
                            ></th>
                    <th>商品</th>
                    <th>图片</th>
                    <th>单价</th>
                    <th>数量</th>
                    <th>小计</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody >
                <tr v-for="item of carts" :key="item.id">
                    <td><input type="checkbox" v-model="item.isSelected"></td>
                    <td>{{item.productName}}</td>
                    <td><img :src="item.productCover" alt=""></td>
                    <td>{{item.productPrice}}</td>
                    <td><input class="count" type="number" v-model.number="item.productCount"></td>
                    <td>{{item.productPrice*item.productCount |handleSum}}</td>
                    <td><button class="btn btn-danger">删除</button></td>
                </tr>
            </tbody>
        </table>
        <p>总计：{{sum}}</p>
        
    </div>
    <script>
        new Vue({
            el:"#app",
            data:{
                carts:[],
            },
            mounted() {
                $.ajax({
                    url:'http://47.108.197.28:8000/cart',
                    success:res=>{
                        console.log(res);
                        this.carts=res
                    }
                })
            },
            filters:{
                handleSum(val){
                    return "$"+val.toFixed(2);
                }
            },
            computed: {
                sum(){
                    var carts=this.carts;
                    var all=0;
                    carts.forEach(item=>{
                        if(item.isSelected){
                            all+=item.productPrice*item.productCount;
                        }
                    })
                    return all.toFixed(2);
                },
                allChecked:{
                    get(){
                        return this.carts.every(item=>item.isSelected)
                    },
                    set(val){
                        this.carts.forEach(item=>{
                            item.isSelected=val
                        })
                    }
                }

            },
            
            
        })
    </script>
</body>
</html>